function TreeNode(val, left, right) {
  this.val = (val === undefined ? 0 : val)
  this.left = (left === undefined ? null : left)
  this.right = (right === undefined ? null : right)
}

function createTree(arr) {
  if (arr.length < 1)
    return null;
  let NodeArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] == null) {
      NodeArr.push(null);
      continue;
    }
    let tempNode = new TreeNode(arr[i], null, null);
    NodeArr.push(tempNode);
  }
  for (let i = 0; i < arr.length; i++) {
    if (NodeArr[i] == null) continue;
    if (2 * i + 1 < arr.length)
      NodeArr[i].left = NodeArr[2 * i + 1];
    if (2 * i + 2 < arr.length)
      NodeArr[i].right = NodeArr[2 * i + 2];
  }
  return NodeArr[0];
}

function preShow(node) {
  if (node == null)
    return;
  console.log(node.val);
  preShow(node.left);
  preShow(node.right);
}

/**
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {boolean}
 */
var isSameTree = function(p, q) {
  // p = createTree(p);
  // q = createTree(q);
  if(p == null && q == null)
    return true;
  if(p == null || q == null)
    return false;
  let flag = true;
  function equalNode(nodeP, nodeQ) {
    if(nodeP == null && nodeQ == null) {
      return;
    }
    if(nodeP == null || nodeQ == null || nodeP.val != nodeQ.val)
      flag = false;
    else {
      equalNode(nodeP.left, nodeQ.left);
      equalNode(nodeP.right, nodeQ.right);
    }
  }
  equalNode(p, q);

  return flag;

};
let p = [1,2,3], q = [1,2,3];
console.log(isSameTree(p, q));
p = [1,2], q = [1,null,2];
console.log(isSameTree(p, q));
p = [1,2,1], q = [1,1,2];
console.log(isSameTree(p, q));
